package com.xbongbong.crm.handle.explain.impl;

import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.parent.interfaces.explain.BusinessExplainHandle;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.saas.enums.OperatePageEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.SalesOpportunityEnum;
import com.xbongbong.saas.help.ExplainHelp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/01/18 17:16
 */
@Service("opportunityExplainHandleImpl")
@Slf4j
public class OpportunityExplainHandleImpl implements BusinessExplainHandle {

    @Resource
    private ExplainHelp explainHelp;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode());
    }

    @Override
    public List<FieldAttrEntity> newHandleExplain(FormatExplainDTO formatExplainDTO) throws XbbException {
        List<FieldAttrEntity> explainList = formatExplainDTO.getExplainList();
        String corpid = formatExplainDTO.getCorpid();
        for (FieldAttrEntity fieldAttrEntity : explainList) {
            String attr = fieldAttrEntity.getAttr();
            if (Objects.equals(attr, SalesOpportunityEnum.PRODUCTS.getAttr())) {
                explainHelp.formatSelectProductExplains(fieldAttrEntity, XbbRefTypeEnum.SALES_OPPORTUNITY, corpid, OperatePageEnum.NEW.getCode(), false);
            }
            if(Objects.equals(attr, SalesOpportunityEnum.LINK_COMPETITOR.getAttr()) || Objects.equals(attr, SalesOpportunityEnum.WIN_COMPETITOR.getAttr())){
                // 新建时关联竞争对手和赢单竞品不支持编辑
                fieldAttrEntity.setEditable(0);
            }
        }
        return explainList;
    }
}
